package com.huadao.cart_ccs.experiment.log.operationLog;

import com.huadao.cart_ccs.data.bo.ExperimentStage;
import com.huadao.cart_ccs.experiment.log.baseLoglist.BaseLogListPresenter;
import com.huadao.cart_ccs.experiment.log.baseLoglist.IBaseLogListContract;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.entity.Operation;
import com.huadao.cart_ccs.greendao.gen.ExperimentStageDao;
import com.huadao.cart_ccs.greendao.gen.OperationDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.Date;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class HumanOperateLogPresenter extends BaseLogListPresenter {

    private OperationDao operationDao;

    public HumanOperateLogPresenter(IBaseLogListContract.IBaseLogListView baseLogListView) {
        super(baseLogListView);
        operationDao=GreenDaoManager.getInstance().getNoCacheSession().getOperationDao();
    }

    @Override
    public void getDataList() {
        getData_dispose=Observable
                .create(new ObservableOnSubscribe<List<Operation>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<Operation>> emitter) throws Exception {
                        skip=0;
                        QueryBuilder queryBuilder=operationDao.queryBuilder().where(OperationDao.Properties.ProjectId.eq(projectId),OperationDao.Properties.StageId.eq(stageId)).orderDesc(OperationDao.Properties.DateTime).limit(count);
                        if(!condition.equals("全部")){
                            queryBuilder.where(OperationDao.Properties.Name.eq(condition));
                        }
                        List<Operation> operationList=queryBuilder.list();
                        skip+=operationList.size();
                        emitter.onNext(operationList);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<Operation>>() {
                    @Override
                    public void accept(List<Operation> o) throws Exception {
                        baseView.showDataList(o);
                    }
                });
    }

    @Override
    public void getMoreData() {
        getData_dispose=Observable
                .create(new ObservableOnSubscribe<List<Operation>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<Operation>> emitter) throws Exception {
                        QueryBuilder queryBuilder=operationDao.queryBuilder().where(OperationDao.Properties.ProjectId.eq(projectId),OperationDao.Properties.StageId.eq(stageId)).orderDesc(OperationDao.Properties.DateTime).offset(skip).limit(count);
                        if(!condition.equals("全部")){
                            queryBuilder.where(OperationDao.Properties.Name.eq(condition));
                        }
                        List<Operation> operationList=queryBuilder.list();
                        skip+=operationList.size();
                        emitter.onNext(operationList);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<Operation>>() {
                    @Override
                    public void accept(List<Operation> o) throws Exception {
                        baseView.showMoreDataList(o);
                    }
                });
    }
}
